package com.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/demo06.do")
public class Demo06Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取到ServletContext对象
        ServletContext servletContext = this.getServletContext();
        //2.获取当前的工程路径
        // /demo20211015
        System.out.println(servletContext.getContextPath());
        //3.获取工程部署在硬盘上的绝对路径
        System.out.println("整个工程的绝对路径" + servletContext.getRealPath("/"));
        System.out.println("hello.html的绝对路径" + servletContext.getRealPath("/hello.html"));
        //4.获取web.xml的context-param
        String bookName = servletContext.getInitParameter("bookName");
        System.out.println(bookName);

        //5.作为域对象使用
        servletContext.setAttribute("book", "钢铁是怎样炼成的");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
